var form = layui.form,groupCode=getQueryVariable("groupCode")
    ,rolecode=getQueryVariable("rolecode");//
var element = layui.element,layer = layui.layer
    ,table = layui.table
var groupName="";
var codes=[];//要添加到组内的角色
queryByGroupCode();//获取角色组名称
form.val("upcharactergroup-form",{
    "groupname":groupName
})
form.render();
//表单初始值，修改时调用
if(rolecode!=""){//角色xode
    $("span")[0].innerHTML="修改角色与角色组关联";
    /*form.val("upcharactergroup-form",{
        "text1":"进入修改角色组",
    })
    setchecked("test1,test2,test3","test");
    form.render();*/
}else{
    $("span")[0].innerHTML="新增角色与角色组关联";
}
table.render({
    elem: '#roleid'
    //,data:data1
    ,   url:"rolegroupmember/selectByGroupCode2ByPage"
    ,toolbar: '#toolbarDemo'
    ,cols: [[
        {checkbox: true, width:50, fixed: true}
        ,{field:'name', title:'角色名称',width: 100, fixed: true}
        ,{field:'code', title:'角色编码',width: 100, fixed: true}
        ,{field:'type', title:'角色类型',width: 100, fixed: true}//角色类型（1:资源；2:标签、按钮）
       // ,{fixed: 'right', width:180, align:'center', toolbar: '#roleeditor', title:'操作'}
    ]]
    ,id: 'roletableid'
    ,height: $(document).height() - $('#roleid').offset().top - 50
    ,page: true
    //,where: {"groupCode":rolecode}  //表格查询条件
    ,done:function(res, curr, count){
        $("[data-field = 'type']").children().each(function(){
            if($(this).text() == '1'){
                $(this).text("资源");
            }else if($(this).text() == '2'){
                $(this).text("标签");
            }
        })

    }
});
table.on('toolbar(roletable)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
        case 'submituser':
            var checkStatus = table.checkStatus(obj.config.id).data;
            if(checkStatus.length == 0){
                layer.msg("请选择要添加的用户")
            }else{

                var usercodes=[];
                for(var i=0;i<checkStatus.length;i++){
                    usercodes.push(checkStatus[i].code);
                }
                var num= insert(groupCode,usercodes);
                if(num <10000 && num >0){
                    layer.msg("保存成功");
                }else if(num <10000 && num ==0){
                    layer.msg("本地保存失败");
                }else if(num >10000  ){
                    layer.msg("同步云枢保存失败");
                }
                tablereload();
            };
            break;
    };
    return false;
});
//监听提交
/*form.on('submit(upcharactergroup-form-element)', function(data){
   // data.field.test = getchecked("test");
    layer.msg("保存成功", {icon: 6});
    setTimeout(function () {
        //保存成功则刷新提交过来的界面 并关闭此页面
        parent.detab("views/jsp/SystemCenter/rolesandrolesgroup.jsp");
        parent.intab("views/jsp/SystemCenter/rolesandrolesgroup.jsp","角色,角色组关联设置")
        parent.detab("views/jsp/SystemCenter/tpl/newupcharactergroup.jsp"+(id==''?'':'?id='+id));
    },1000)
    return false;
});*/
//行点击
function  tablereload() {
    table.reload('roletableid', {
        //data:data1,
        url:"rolegroupmember/selectByGroupCodeByPage",
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: {"groupCode":groupCode}  //表格查询条件
    }, 'data'); //表格刷新
}
function insert(groupCode,usercodes) {
    var tf=false;
    $.ajax({
        type : 'POST' ,
        data: {"groupCode":groupCode,"usercodes":usercodes},
        url:'rolegroupmember/insert2',
        dataType:"json",
        //contentType: 'application/json',
        async:false,
        traditional: true,
        success : function (da) {
            if(da.status==200 ){

                //tf=da.data==0?false:true;
                tf=da.data;
            }
        },
        error: function(data) {
            layer.msg("保存失败!");
        }
    });
    return tf;
}
//获取角色组列表
function  queryByGroupCode() {
    $.ajax({
        type : 'GET' ,
         data: {"groupCode":groupCode},
        url:'rolegroup/selectByCode',
        dataType:"json",
        //contentType: 'application/json',
        async:false,
        traditional: true,
        success : function (da) {
            if(da.status==200 ){
                //  obj=da.data==0?false:true;
               // rolegroup=da.data;
                groupName=da.data;
            }
        },
        error: function(data) {
            layer.msg("查询失败!");
        }
    });
}